import { _decorator, Component, Node, sp } from 'cc';
import { GameDefine } from '../GameDefine';
const { ccclass, property } = _decorator;

@ccclass('lightEffect')
export class lightEffect extends Component {
    @property({ type: [sp.Skeleton] })
    effectAnimList: sp.Skeleton[] = [];
    private moveSpeed: number = 30;

    onLoad() {
        // 确保初始都为 inactive
        for (const n of this.effectAnimList) {
            if (n) n.node.active = false;
        }
    }

    start() {
        const length = this.effectAnimList.length;
        if (length == 0)
            return;


        // 监听最后一个 Spine skeleton 的播放完成回调（play complete）
        const lastSk = this.effectAnimList[length - 1];
        if (lastSk && (lastSk as any).setCompleteListener) {
            (lastSk as any).setCompleteListener(() => {
                console.log("特效播放完成")
                this.node.destroy();
            });
        }

        // 以 0.1s 的间隔依次激活列表中的节点
        const interval = 0.1;
        for (let i = 0; i < this.effectAnimList.length; i++) {
            const anim = this.effectAnimList[i];
            if (!anim) continue;
            this.scheduleOnce(() => {
                anim.node.active = true;
            }, interval * i);
        }

    }

    update(dt: number) {
        if (GameDefine.isGamePaused)
            return;

        const moveStep = this.moveSpeed * dt;
        const p = this.node.position;
        this.node.setPosition(p.x - moveStep, p.y, p.z);
    }
}


